package com.feng.vhr.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.feng.vhr.common.RespBean;
import com.feng.vhr.common.UpdatePwd;
import com.feng.vhr.pojo.Hr;
import com.feng.vhr.response.RHr;
import com.feng.vhr.service.IHrRoleService;
import com.feng.vhr.service.IHrService;
import com.feng.vhr.service.IRoleService;
import com.feng.vhr.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/hr")
public class HrController {

    @Autowired
    private IHrService hrService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IHrRoleService hrRoleService;

    /**
     * 获取个人信息
     *
     * @return
     */
    @GetMapping("/info")
    public RespBean info(HttpSession session) {
        RHr rHr = new RHr();
        RHr id = (RHr) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        QueryWrapper<HrRole> wrapper = new QueryWrapper<>();
//        wrapper.eq("hrid",id);
//        List<HrRole> hrRoles = hrRoleService.list(wrapper);
//        List<Role> roles = new ArrayList<>();
//        roles=hrRoles.stream().map(item->{
//            Role role = roleService.getById(item.getRid());
//            return role;
//        }).collect(Collectors.toList());
//        BeanUtils.copyProperties(user,rHr);
//        rHr.setRoles(roles);
        return RespBean.ok("获取信息成功", id);
    }

    /**
     * 修改信息
     *
     * @param rHr
     * @return
     */
    @PutMapping("/info")
    public RespBean update(@RequestBody RHr rHr, Authentication authentication) {
        Hr hr = new Hr();
        BeanUtils.copyProperties(rHr, hr);
        boolean update = hrService.updateById(hr);
        if (update) {
            SecurityContextHolder.getContext().setAuthentication(
                    new UsernamePasswordAuthenticationToken(rHr, authentication.getCredentials(), authentication.getAuthorities()));
            return RespBean.ok("修改成功");

        }
        return RespBean.error("修改失败");
    }

    /**
     * 修改密码
     *
     * @param updatePwd
     * @return
     */
    @PutMapping("/pass")
    public RespBean updatePassword(@RequestBody UpdatePwd updatePwd) {
        Hr hr = hrService.getById(updatePwd.getHrid());
        String oldPass = MD5.encrypt(updatePwd.getOldpass());
        if (hr.getPassword().equals(oldPass)) {
            if (updatePwd.getPass().equals(updatePwd.getCheckPass())) {
                String newPwd = MD5.encrypt(updatePwd.getCheckPass());
                hr.setPassword(newPwd);
                hrService.updateById(hr);
                return RespBean.ok("密码修改成功！");
            } else {
                return RespBean.error("两次密码输入不一致！");
            }
        } else {
            return RespBean.error("请输入正确的旧密码！");
        }
    }

    /**
     * 修改用户头像
     *
     * @param file
     * @param id
     * @return
     */
    @PostMapping("/userface")
    public RespBean updateUserFace(MultipartFile file, Integer id, Authentication authentication) {

        try {
            String oldName = file.getOriginalFilename();
            String substring = oldName.substring(oldName.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = fileName + substring;
            String path = "E:\\java\\nginx-1.18.0\\html\\userface\\";
            File files = new File(path + fileName);
            if (!files.getParentFile().exists()) {
                files.getParentFile().mkdirs();
            }
            file.transferTo(files);
            UpdateWrapper<Hr> hrUpdateWrapper = new UpdateWrapper<>();
            hrUpdateWrapper.set("userface", "http://localhost/userface/" + fileName).eq("id", id);
            boolean update = hrService.update(hrUpdateWrapper);
            if (update) {
                RHr rhr = (RHr) authentication.getPrincipal();
                rhr.setUserface("http://localhost/userface/" + fileName);
                SecurityContextHolder.getContext().setAuthentication(
                        new UsernamePasswordAuthenticationToken(rhr, authentication.getCredentials(), authentication.getAuthorities()));
                return RespBean.ok("头像更新成功", "http://localhost/userface/" + fileName);
            }
            return RespBean.error("头像更新失败");
        } catch (IOException e) {
            e.printStackTrace();
            return RespBean.error("头像更新失败");
        }
    }
}
